/**
 * @Author Puzzle
 * @Date 2022/6/13 11:21 上午
 **/

package model

import (
	"github.com/pkg/errors"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"zsc-boot/core/orm"
)

type ServiceUser struct {
	orm.Model
	Name           string                  `json:"name" form:"name" gorm:"column:name;comment:名称;size:128"`
	Login          string                  `json:"login" form:"login" gorm:"column:login;comment:账号;size:32"`
	Password       string                  `json:"password"  gorm:"column:password;comment:密码;size:64"`
	IdentNumber    string                  `json:"identNumber" form:"identNumber" gorm:"column:ident_number;comment:身份证号;size:64"`
	Mobile         string                  `json:"mobile" form:"mobile" gorm:"column:mobile;comment:联系方式;size:32"`
	RoleId         uint                    `json:"roleId" gorm:"column:role_id;comment:角色标识;"`
	IsActive       bool                    `json:"isActive" gorm:"column:is_active;comment:是否在职(是、否);default:true"`
	Role           ServiceRole             `json:"role" form:"role" gorm:"foreignKey:RoleId;"`
	City           string                  `json:"city" gorm:"column:city;comment:城市;default:''"`
	AndroidVersion string                  `json:"androidVersion" gorm:"column:android_version;default:'';comment:安卓版本;size:64"`
	CustomerCap    uint                    `json:"customerCap" gorm:"column:customer_cap;default:0;comment:客户容量"`
	DayCap         uint                    `json:"dayCap" gorm:"column:day_cap;default:0;comment:每日配额"`
	ToDayLen       uint                    `json:"toDayLen" gorm:"column:today_len;default:0;comment:今日已使用配额"`
	CustomerLen    uint                    `json:"customerLen" gorm:"column:customer_len;default:0;comment:客户数量"`
	ServiceDate    string                  `json:"serviceDate" gorm:"column:service_date;default:'';comment:入职日期;size:32"`
	SubUsers       []ServiceUserSimple     `gorm:"-"`
	DepId          uint                    `json:"depId" form:"depId" gorm:"column:dep_id;comment:部门id;"`
	DepInfo        ServiceDepartmentSimple `json:"depInfo" gorm:"foreignKey:DepId;"`
	FollowNum      uint                    `json:"followNum" gorm:"-"`
}

// AfterUpdate 在同一个事务中更新数据
func (ServiceUser) AfterUpdate(tx *gorm.DB) (err error) {
	zap.L().Warn("发生了修改", zap.Error(errors.New("未知修改")), zap.Stack("未知修改"))
	return
}

func (ServiceUser) TableName() string {
	return "service_user"
}

func (ServiceUser) TableComment() string {
	return "用户"
}

type UserCustomer struct {
	orm.Model
	UserId uint `json:"userId" gorm:"column:user_id;comment:归属员工id;index:inx_user_id;default:0"`
}

func (UserCustomer) TableName() string {
	return "customer"
}

type CustomerFlowLog struct {
	orm.Model
	CustomerId  uint              `json:"customerId" gorm:"column:customer_id;index:inx_customer_id"`
	NewUserId   uint              `json:"newUserId" gorm:"column:new_user_id;default:0;"`
	OldUserInfo ServiceUserSimple `json:"oldUserInfo" gorm:"foreignKey:OldUserId"`
	NewUserInfo ServiceUserSimple `json:"newUserInfo" gorm:"foreignKey:NewUserId"`
	OldUserId   uint              `json:"oldUserId" gorm:"column:old_user_id;default:0;"`
	Reason      string            `gorm:"column:reason;comment:流转原因;size:128"`
}

func (CustomerFlowLog) TableName() string {
	return "customer_flow_log"
}

type ServiceUserSimple struct {
	ID        uint           `json:"ID" form:"ID" gorm:"column:id;primaryKey;comment:主键ID"`                              // 主键ID
	CreatedAt orm.LocalTime  `json:"createdAt"  time_format:"2006-01-02 15:04:05" gorm:"column:created_at;comment:创建时间"` // 创建时间
	UpdatedAt orm.LocalTime  `json:"updatedAt" time_format:"2006-01-02 15:04:05" gorm:"column:updated_at;comment:更新时间"`  // 更新时间
	DeletedAt gorm.DeletedAt `json:"-" form:"-" gorm:"column:deleted_at;comment:删除时间;index"`
	Name      string         `json:"name" form:"name" gorm:"column:name;comment:名称"`
	DepId     uint           `json:"depId" gorm:"column:dep_id;comment:部门id;"`
}

func (ServiceUserSimple) TableName() string {
	return "service_user"
}
